﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace DiscoveryLogic.DataViz.NetworkDiagram.Measures
{
    public partial class GraphMeasureSettings : Form
    {
        public GraphMeasureSettings()
        {
            InitializeComponent();
        }

        private void GraphMeasureSettings_Load(object sender, EventArgs e)
        {
            this.cbo_MeasureTypes.Items.Clear();
            for(int i=0;i<=2;i++)
            {
                MeasureType measureType = (MeasureType) i;
                this.cbo_MeasureTypes.Items.Add(measureType.ToString());
            }
            Assembly assembly = this.GetType().Assembly;
            Type[] types = assembly.GetTypes();
            this.cbo_MeasureNames.Items.Clear();
            List<string> measureNames=new List<string>();
            foreach(Type type in types)
            {
                if(type.GetInterface(typeof(IMeasure).FullName)!=null && type.IsClass && (!type.IsAbstract))
                {
                    IMeasure measure = (IMeasure) Activator.CreateInstance(type);
                    if(!measureNames.Contains(measure.MeasureName))
                        measureNames.Add(measure.MeasureName);
                }
            }
            measureNames.Sort();
            foreach(string measureName in measureNames)
            {
                this.cbo_MeasureNames.Items.Add(measureName);
            }
        }

        private void cbo_MeasureNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_MeasureNames.SelectedIndex>=0)
            {
                string measureName = this.cbo_MeasureNames.SelectedItem.ToString();
                Assembly assembly = this.GetType().Assembly;
                Type[] types = assembly.GetTypes();
                this.cbo_Implementations.Items.Clear();
                List<string> classNames = new List<string>();
                foreach (Type type in types)
                {
                    if (type.GetInterface(typeof(IMeasure).FullName) != null && type.IsClass && (!type.IsAbstract))
                    {
                        IMeasure measure = (IMeasure)Activator.CreateInstance(type);
                        if(measure.MeasureName==measureName)
                            classNames.Add(type.FullName);
                    }
                }
                classNames.Sort();
                foreach(string className in classNames)
                {
                    this.cbo_Implementations.Items.Add(className);
                }
            }
        }

        private void cbo_Implementations_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Implementations.SelectedIndex>=0)
            {
                Assembly assembly = this.GetType().Assembly;
                string typeName = this.cbo_Implementations.SelectedItem.ToString();
                Type type = assembly.GetType(typeName);
                IMeasure measure =(IMeasure) Activator.CreateInstance(type);
                this.txt_Algorithm.Text = measure.Algorithm;
                this.rt_Description.Text = measure.Description;
            }
            
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {

        }
    }
}
